package com.corpgovernment.organization.dataloader.db;

import com.corpgovernment.organization.entity.db.OrganizationChangeHistory;
import com.corpgovernment.organization.mapper.OrganizationChangeHistoryMapper;
import com.corpgovernment.organization.model.OrganizationChangeHistoryModel;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zl.cui
 * @date 2023/9/19
 */
@Slf4j
@Component
public class OrganizationChangeHistoryLoader {
    @Autowired
    private OrganizationChangeHistoryMapper organizationChangeHistoryMapper;

    public int batchSave(List<OrganizationChangeHistoryModel> modelList){
        if (CollectionUtils.isEmpty(modelList)){
            return 0;
        }
        List<OrganizationChangeHistory> historyList = modelList.stream()
                .filter(Objects::nonNull)
                .map(this::convert)
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(historyList)){
            return 0;
        }
        return organizationChangeHistoryMapper
                .batchInsert(historyList);
    }

    private OrganizationChangeHistory convert(OrganizationChangeHistoryModel model){
        OrganizationChangeHistory organizationChangeHistory = new OrganizationChangeHistory();
        organizationChangeHistory.setEmployeeId(model.getEmployeeId());
        organizationChangeHistory.setBeforeOrgId(model.getBeforeOrgId());
        organizationChangeHistory.setCurrentOrgId(model.getCurrentOrgId());
        organizationChangeHistory.setOperateType(model.getOperateType());
        return organizationChangeHistory;
    }
}
